import { defineStore } from 'pinia'
import { ref } from 'vue'
import { loginApi } from '@/api/user'
import type { LoginType, LoginRes } from '@/types/user'

const useUserStore = defineStore(
  'user',
  () => {
    // 用户信息
    // as: 类型断言
    // ！：非空断言
    const userInfo = ref({} as LoginRes)

    // 登录
    const login = async (data: LoginType) => {
      const res = await loginApi(data)
      userInfo.value = res.data.data
    }

    // 退出
    const logout = ()=>{
      userInfo.value = {} as LoginRes
    }

    return { userInfo, login ,logout}
  },
  {
    persist: true,
  },
)

export default useUserStore
